﻿<%@ WebHandler Language="C#" Class="ReportTracking.Services.Chart.PL2_Chart1" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using DocumentFormat.OpenXml.Drawing.Charts;
using System.Data;
namespace ReportTracking.Services.Chart
{
    /// <summary>
    /// Summary description for PL2_Chart1
    /// </summary>
    public class PL2_Chart1 : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";

                /* Lay du lieu o day */
                InfoReport inforeport = (InfoReport)context.Session["InfoReprot"];
                System.Data.DataTable tb = (System.Data.DataTable)context.Session["Data"];
                var results = from myRow in tb.AsEnumerable()
                              group myRow by myRow.Field<string>("BusinessUnitType") into g
                              select new { LHDV = g.Key, Numbers = g.Count() };
                System.Collections.Hashtable htb = new System.Collections.Hashtable();

                int nTotal = inforeport.Total;
                foreach (var r in results)
                {
                    htb.Add(r.LHDV, String.Format("{0:0.00}",((double)r.Numbers / nTotal)));

                }

                /* KT lay du lieu o day*/

                StringBuilder sData = new StringBuilder();
                sData.Append("{");
                sData.Append("\"Title\":");
                sData.Append("\"Thống kê loại hình dịch vụ:");
                sData.Append(inforeport.FromDay);
                sData.Append(" đến: ");
                sData.Append(inforeport.ToDay);
                sData.Append("\"");

                sData.Append(",");
                sData.Append("\"Values\":");
                sData.Append("[{");

                sData.Append("\"type\":");
                sData.Append("\"pie\",");
                sData.Append("\"name\":");
                sData.Append("\"Loại hình dịch vụ\",");
                sData.Append("\"data\":");
                sData.Append("[");

                /*Begin loop*/
                System.Collections.ArrayList coll = new System.Collections.ArrayList(htb.Keys);
                if (htb.Count > 1)
                {

                    for (int i = 0; i < coll.Count - 1; i++)
                    {

                        sData.Append("[");
                        sData.Append("\"");
                        sData.Append(coll[i]);
                        sData.Append("\"");
                        sData.Append(",");
                        sData.Append(htb[coll[i]]);
                        sData.Append("]");
                        sData.Append(",");
                    }

                }
                sData.Append("{");
                sData.Append("\"name\"");
                sData.Append(":");
                sData.Append("\"");
                sData.Append(coll[coll.Count - 1]);
                sData.Append("\"");
                sData.Append(",");
                sData.Append("\"y\"");
                sData.Append(":");
                sData.Append(htb[coll[coll.Count - 1]]);
                sData.Append(",");
                sData.Append("\"sliced\"");
                sData.Append(":");
                sData.Append("true");
                sData.Append(",");
                sData.Append("\"selected\"");
                sData.Append(":");
                sData.Append("true");
                sData.Append("}");

                /*
                sData.Append("[");
                sData.Append("\"Firefox\"");
                sData.Append(",");
                sData.Append("45.0");
                sData.Append("]");
                sData.Append(",");

                sData.Append("[");
                sData.Append("\"IE\"");
                sData.Append(",");
                sData.Append("26.8");
                sData.Append("]");
                sData.Append(",");

                sData.Append("[");
                sData.Append("\"Safari\"");
                sData.Append(",");
                sData.Append("8.5");
                sData.Append("]");
                sData.Append(",");

                sData.Append("[");
                sData.Append("\"Opera\"");
                sData.Append(",");
                sData.Append("6.2");
                sData.Append("]");
                sData.Append(",");

                sData.Append("[");
                sData.Append("\"Others\"");
                sData.Append(",");
                sData.Append("0.7");
                sData.Append("]");
                sData.Append(",");

                sData.Append("{");
                sData.Append("\"name\"");
                sData.Append(":");
                sData.Append("\"Chrome\",");
                sData.Append("\"y\"");
                sData.Append(":");
                sData.Append("12.8");
                sData.Append(",");
                sData.Append("\"sliced\"");
                sData.Append(":");
                sData.Append("true");
                sData.Append(",");
                sData.Append("\"selected\"");
                sData.Append(":");
                sData.Append("true");
                sData.Append("}");
                 */

                /*End loop*/
                sData.Append("]");

                sData.Append("}]");
                sData.Append("}");

                context.Response.Write(Newtonsoft.Json.JsonConvert.DeserializeObject(sData.ToString()));

            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}